package com.flight.trans.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.flight.trans.R;
import com.flight.trans.adapter.EnterPortHandoverAdapter;
import com.flight.trans.base.BaseActivity;
import com.flight.trans.bean.HandoverBean.HandoverData;
import com.flight.trans.global.Constant;
import com.flight.trans.http.NetworkUtils;
import com.flight.trans.http.network.OkGoBackListener;
import com.flight.trans.listener.HandoverConfirmListener;
import com.flight.trans.listener.OnItemClickListener;
import com.flight.trans.listener.OnTextClickListener;
import com.flight.trans.util.CommonUtils;
import com.flight.trans.view.dialog.EnterPortHandoverDialog;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : YanKing
 * @date : 2019/5/19  9:53
 * @Description : 进港司机交接
 */
public class EnterPortHandoverActivity extends BaseActivity {

    private LinearLayout mLlContainer ;
    private Button mBtnAdd;//新增
    private TextView mTvTitleName ;
    private RecyclerView mRlContainer ;

    private EnterPortHandoverAdapter mHandoverAdapter ;
    private List<HandoverData> mHandoverDataList;
    private int mFlightID ;

    @Override
    protected int setContentView() {
        return R.layout.activity_enter_port_handover;
    }

    @Override
    protected void initView() {
        mLlContainer = findViewById(R.id.mLlContainer);
        mBtnAdd = findViewById(R.id.mBtnAdd);
        mTvTitleName = findViewById(R.id.mTvTitleName);
        mRlContainer = findViewById(R.id.mRlContainer);
    }

    @Override
    protected void initData() {
        IsEditor = mBundle.getBoolean(Constant.IsEditor);
        mFlightID = mBundle.getInt(Constant.FlightID);

        mBtnAdd.setVisibility(IsEditor ? View.VISIBLE : View.GONE);

        mTvTitleName.setText(getString(R.string.title_enter_port_handover));
        CommonUtils.onFinishTouch(mActivity , mLlContainer);
    }

    @Override
    protected void initConnect() {
        NetworkUtils.getHandoverList(new OkGoBackListener(mActivity) {
            @Override
            public void onSuccess(Object body) {
                mHandoverDataList = (List<HandoverData>) body;

                initAdapter();
            }
        }, mFlightID , true);
    }

    private void initAdapter(){
        if(mHandoverAdapter == null){
            mHandoverAdapter = new EnterPortHandoverAdapter(mActivity , mHandoverDataList);
            mRlContainer.setAdapter(mHandoverAdapter);
            mRlContainer.setLayoutManager(new LinearLayoutManager(mActivity));

            mHandoverAdapter.setOnTextClickListener(new ItemTextClickListener());
            mHandoverAdapter.setOnItemClickListener(new ItemOnClickListener());
        }else{
            mHandoverAdapter.updateAdapter(mHandoverDataList);
        }
    }

    /**
     * 条目点击事件
     */
    private class ItemOnClickListener extends OnItemClickListener{
        @Override
        public void onClick(View view, int position) {
            EnterPortHandoverDialog.getInstance(mActivity)
                    .setData(mHandoverDataList.get(position) , IsEditor)
                    .setConfirmButton(new OnHandoverConfirmListener())
                    .show();
        }
    }

    /**
     * 条目点击事件
     */
    private class ItemTextClickListener implements OnTextClickListener{
        @Override
        public void onClick(View view, int position) {
            switch (view.getId()){
                case R.id.mIvDriverSign:
                    // 司机签字
                    ArrayList imageList = new ArrayList<String>();
                    imageList.add(mHandoverDataList.get(position).getUrl());

                    mIntent = new Intent(mActivity , SeePicActivity.class);
                    mBundle = new Bundle();
                    mBundle.putStringArrayList(Constant.INFO , imageList);
                    mBundle.putInt(Constant.POSITION , 0);
                    mIntent.putExtras(mBundle);
                    turnActForResult(mIntent);
                    break;
            }
        }
    }

    public void onClick(View view){
        if(CommonUtils.isFastDoubleClick())
            return;

        switch (view.getId()){
            case R.id.mBtnAdd:
                // 新增
                HandoverData handoverData = new HandoverData();
                handoverData.setFlightId(mFlightID);
                EnterPortHandoverDialog.getInstance(mActivity)
                        .setData(handoverData , IsEditor)
                        .setConfirmButton(new OnHandoverConfirmListener())
                        .show();
                break;
            case R.id.mIbBack:
                onFinish();
                break;
        }
    }

    private class OnHandoverConfirmListener implements HandoverConfirmListener {
        @Override
        public void onClick(int code) {
            initConnect();
        }
    }
}
